﻿using BackupContracts.Tasks;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight
{
	public partial class EditTaskScheduleUserControl : UserControl
	{
		public EditTaskScheduleUserControl()
		{
			// Required to initialize variables
			InitializeComponent();
            ScheduleTypeCombo.Items.Add(ScheduleType.OneTime);
            ScheduleTypeCombo.Items.Add(ScheduleType.Daily);
            ScheduleTypeCombo.Items.Add(ScheduleType.Weekly);
            ScheduleTypeCombo.Items.Add(ScheduleType.Monthly);
            DataContextChanged += EditTaskScheduleUserControl_DataContextChanged;
        }

        void EditTaskScheduleUserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (Model != null)
            {
                Model.PropertyChanged += Model_PropertyChanged;
                UpdateScheduleTypeOptions();
            }
        }

        void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("ScheduleType"))
                UpdateScheduleTypeOptions();
        }

        private void UpdateScheduleTypeOptions()
        {
            DaysOfWeekCombo.Visibility = Visibility.Collapsed;
            MonthsCombo.Visibility = Visibility.Collapsed;
            DaysCombo.Visibility = Visibility.Collapsed;
            DaysOfWeekText.Visibility = Visibility.Collapsed;
            MonthsText.Visibility = Visibility.Collapsed;
            DaysText.Visibility = Visibility.Collapsed;
            RepeatBox.Visibility = Visibility.Collapsed;
            RepeatText.Visibility = Visibility.Collapsed;
            switch (Model.ScheduleType)
            {
                case ScheduleType.OneTime:
                    break;
                case ScheduleType.Daily:
                    RepeatBox.Visibility = Visibility.Visible;
                    RepeatText.Visibility = Visibility.Visible;
                    break;
                case ScheduleType.Weekly:
                    RepeatBox.Visibility = Visibility.Visible;
                    RepeatText.Visibility = Visibility.Visible;
                    DaysOfWeekCombo.Visibility = Visibility.Visible;
                    DaysOfWeekText.Visibility = Visibility.Visible;
                    break;
                case ScheduleType.Monthly:
                    MonthsCombo.Visibility = Visibility.Visible;
                    DaysCombo.Visibility = Visibility.Visible;
                    MonthsText.Visibility = Visibility.Visible;
                    DaysText.Visibility = Visibility.Visible;
                    break;
            }
        }

        private Schedule Model
        {
            get 
            {
                return DataContext as Schedule;
            }
        }
	}
}